#include <cstdio>
#include <cstring>

struct Node
{
    Node *next;
    int id;
};

int n, m, c[ 100001 ], v[ 100001 ];
int d[ 505 ][ 10005 ];
Node *li[ 100001 ];

void init( )
{
    int i, f;
    Node *p;
    memset( li, NULL, sizeof( li ) );
    for ( i = 1; i <= n; i++ )
    {
        scanf("%d%d%d", &c[ i ], &v[ i ], &f);
        if ( f == i )
            f = 0;
        if ( !li[ f ] )
        {
            li[ f ] = new Node;
            li[ f ]->id = i;
            li[ f ]->next = NULL;
        }
        else
        {
            p = new Node;
            p->id = i;
            p->next = li[ f ]->next;
            li[ f ]->next = p;
        }
    }
}

void dp( int p, int ca, int step )
{
    int i, j;
    Node *ii;
    for ( ii = li[ p ]; ii; ii = ii->next )
    {
        i = ii->id;
        if ( ca - c[ i ] < 0 ) continue;
        for ( j = 0; j <= m; j++ ) d[ step + 1 ][ j ] = d[ step ][ j ];
        dp( i, ca - c[ i ], step + 1 );
        for ( j = c[ i ]; j <= ca; j++ )
            if ( d[ step ][ j ] < d[ step + 1 ][ j - c[ i ] ] + v[ i ] )
                d[ step ][ j ] = d[ step + 1 ][ j - c[ i ] ] + v[ i ];
    }
}

void work( )
{
    int i, ans = 0;
    memset( d[ 0 ], 0, sizeof( d[ 0 ] ) );
    dp( 0, m, 0 );
    for ( i = 0; i <= m; i++ )
        if ( ans < d[ 0 ][ m ] )
            ans = d[ 0 ][ m ];
    printf("%d\n", ans);
}

int main( )
{
    while ( scanf("%d%d", &n, &m) != EOF )
    {
        init( );
        work( );
    }
    return 0;
}
